package ute.mvc.controller.portal.lecturer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import ute.mvc.model.Question;
import ute.mvc.model.QuestionPool;
import ute.mvc.model.TestPart;
import ute.mvc.service.OpenCourseService;
import ute.mvc.service.QuestionPoolService;
import ute.mvc.service.QuestionService;
import ute.mvc.service.TestPartService;
import ute.mvc.service.UserService;
import ute.mvc.util.IdUtil;

@Controller
@RequestMapping(value = {
		"/lecturer/{lecturerId}/{openCourseId}/{subjectId}/pool/{poolId}/question",
		"/lecturer/{lecturerId}/{openCourseId}/{subjectId}/{testId}/part/{partId}/question" })
public class QuestionController {

	@Autowired
	QuestionPoolService poolService;
	@Autowired
	QuestionService questionService;
	@Autowired
	TestPartService partService;
	@Autowired
	UserService userService;
	@Autowired
	OpenCourseService courseService;

	/**
	 * Get question OK
	 * 
	 * @param questionId
	 * @return
	 */
	@RequestMapping(value = "/{questionId}", method = RequestMethod.GET)
	@ResponseBody
	public Question getEditQuestion(@PathVariable String questionId) {

		System.out.println("Get question: " + questionId);
		Question question = questionService.getQuestion(questionId);
		return question;
	}

	/**
	 * Add question To Part OK
	 * 
	 * @param id
	 * @param question
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public Question addQuestionToPart(@PathVariable String partId,
			@RequestBody Question question) {

		System.out.println("Add question in part: " + partId);
		TestPart part = partService.getTestPartById(partId);

		// Generate Id
		String maxId = questionService.getMaxIdInPart(part);
		String newId = IdUtil.getNextId(maxId);
		question.setQuestionId(partId + "_" + newId);

		question.setTestPart(part);
		questionService.addQuestion(question);
		return question;
	}

	/**
	 * Add question in Pool
	 * 
	 * @param id
	 * @param question
	 * @return
	 */
	@RequestMapping(value = "/addToPool", method = RequestMethod.POST)
	@ResponseBody
	public Question addQuestionToPool(@PathVariable String poolId,
			@RequestBody Question question) {
		// Test OK
		System.out.println("Add question in pool " + poolId);
		QuestionPool pool = poolService.getQuestionPool(poolId);
		question.setQuestionPool(pool);

		// Generate Id for question in pool
		String maxId = questionService.getMaxIdInPool(pool);

		// Sequence number of question in pool
		String newId = IdUtil.getNewQuestionIdInPool(maxId);
		question.setQuestionId(poolId + "_" + newId);
		questionService.addQuestion(question);
		return question;
	}

	/**
	 * Update question OK in Test
	 * 
	 * @param id
	 * @param question
	 * @return
	 */
	@RequestMapping(value = "/edit/{questionId}", method = RequestMethod.POST)
	@ResponseBody
	public Question editQuestion(@PathVariable String questionId,
			@RequestBody Question question) {
		System.out.println("Update question: " + questionId);
		Question oldQuestion = questionService.getQuestion(questionId);
		question.setQuestionPool(oldQuestion.getQuestionPool());
		question.setTestPart(oldQuestion.getTestPart());
		questionService.updateQuestion(question);
		return question;
	}

	/**
	 * Delete question OK in Test
	 */
	@RequestMapping(value = "/delete/{questionId}", method = RequestMethod.GET)
	@ResponseBody
	public String deleteQuestion(@PathVariable String questionId) {
		// Test OK
		System.out.println("Delete: " + questionId);
		Question question = questionService.getQuestion(questionId);
		questionService.deleteQuestion(question);
		return "OK";
	}

}
